#
# genircode.sh -Anders 2009_0709
# semi-automatic code, just execute like this:
# anders@linux#mode2 -d /dev/lircd0 | ./genircode.sh IRFREQ OUTFILE
#

#!/bin/sh

let interval=2
head="const struct powercode xxxx PROGMEM = {\n\tfreq_to_timerval(%d),"

function usage()
{
        echo -e "\nusage: genircode IRFREQ OUTFILE\n"
}

function add_entry()
{
        if [ -e $2 ]; then
                printf "$head" $1 >> $2
        else
                printf "$head" $1 > $2
        fi
}

function add_data()
{
	digit=$2
	digit=$(($digit/10))

	if [ $1 = "pulse" ]; then	
		printf "\n\t{%d, " $digit >> $3
	fi

	if [ $1 = "space" ]; then	
		printf "%d}," $digit >> $3
	fi
}

if [ $# -ne 2 ]; then
        usage
        exit 0
fi

first=1
freq=$1
of=$2

start=$(date +%s);

while read line; do
        now=$(date +%s)
	echo $now-$start=$((now-$start))
        if [ $(($now-$start)) -gt $interval ] || [ $first -eq 1 ]; then
		add_entry $freq $of
		first=0
        fi
        start=$(date +%s)
	add_data $line $of
        echo $line
done

